package com.gule.gl.leetcode.editor.cn;

import com.gule.gl.leetcode.editor.cn.definition.ListNode;

import java.util.Objects;

/**
 * 反转单链表
 */
public class LinkedListReverse {
    public static void main(String[] args) {
        Solution solution = new LinkedListReverse().new Solution();

    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * class ListNode {
     * int val;
     * ListNode next;
     * ListNode(int x) {
     * val = x;
     * next = null;
     * }
     * }
     */
    public class Solution {

        /**
         * 主要是需要记录反转前的node节点，不能让链表断开
         * 单链表反转
         *
         * @param head
         * @return
         */
        public ListNode hasCycle(ListNode head) {
            if (Objects.isNull(head) || Objects.isNull(head.next)) {
                return null;
            }
            ListNode pNode = head;
            ListNode reverseHeadNode = head;
            ListNode pPrev = null;
            while (Objects.nonNull(pNode)) {
                //pNode是当前节点 即 pPrev
                //pNode.next是下一个节点 即 pNext
                ListNode pNext = pNode.next;
                if (Objects.isNull(pNext)) {
                    reverseHeadNode = pNode;
                }
                pNode.next = pPrev;
                pPrev = pNode;

                pNode = pNext;
            }
            return reverseHeadNode;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
